import os
import requests
from lxml import etree # import lxml.etree as le

keyword = input('请输入关键字：')
start_page = input('请输入起始页：')
end_page = input('请输入终止页：')

for i in range(int(start_page), int(end_page)+1):
    url = 'https://so.csdn.net/so/search/s.do?p=%s&q=%s&t=blog&viparticle=&domain=&o=&s=&u=&l=&f=&rbg=0' % (str(i), keyword)
    root = r'E:\Users\zfl\PycharmProjects\Work'
    path = root + '/' + keyword + '_page_' + str(i) + '.html'
    # print(path)
    # try:
    #     if not os.path.exists(root):
    #         os.mkdir(root)
    #     if not os.path.exists(path):
    #         r = requests.get(url)
    #         with open(path, 'wb') as f:
    #             f.write(r.content)
    #             print('file saves successfully')
    #     else:
    #         print('the dir exists.')
    # except:
    #     print('failed')
    html = requests.get(url).content
    urls = etree.HTML(html).xpath('//span[@class="link"]/a/text()')
    print(urls)
    print(len(urls))
    for url in urls:
        rep = requests.get()
        with open(r'E:\Users\zfl\PycharmProjects\Work','wb')as f:
            f.write(rep.content)
            print('file saves successfully')

